#!/usr/bin/python
# -*- coding: utf8 -*-

import re
from pysqlite2 import dbapi2 as sqlite

class User(object):
   def __init__(self):
      self.id       = 0
      self.jid      = u''
      self.daterid  = u''
      self.password = u''

   def fromJID(self, cursor, jid):
      cursor.execute('SELECT id, jid, daterid, password from User WHERE jid="%s"' % (jid))
      data = cursor.fetchone()
      if len(data):
         self.id, self.jid, self.daterid, self.password = data
         return True
      else:
         return False

   def fromID(self, cursor, id):
      cursor.execute('SELECT id, jid, daterid, password from User WHERE id="%s"' % (id))
      data = cursor.fetchone()
      if len(data):
         self.id, self.jid, self.daterid, self.password = data
         return True
      else:
         return False

class Dater(object):
   def __init__(self):
      self.login = u''
      self.oid   = u''
      self.id    = 0
      self.rating = 0

   def fromLogin(self, cursor, login):
      cursor.execute('SELECT id, login, oid, rating from Dater WHERE login="%s"' % (login))
      data = cursor.fetchone()
      if len(data):
         self.id, self.login, self.oid, self.rating = data
         return True
      else:
         return False

   def fromID(self, cursor, id):
      cursor.execute('SELECT id, login, oid, rating from Dater WHERE id="%s"' % (id))
      data = cursor.fetchone()
      if len(data):
         self.id, self.login, self.oid, self.rating = data
         return True
      else:
         return False

   def inDB(self, cursor):
      cursor.execute('SELECT id from Dater WHERE login = "%s"' % (self.login))
      data = cursor.fetchone()
      if data:
         return True
      else:
         return False

   def Append(self, cursor):
      if not self.inDB(cursor):
         cursor.execute('INSERT INTO Dater VALUES(null, ?, ?, 0)',
                        (self.login, self.oid))


class Collector(object):
   def __init__(self):
      self.db = 0
      self.search = 0
      self.re_secure_login = re.compile(u'(?iu)[a-z_0-9]+')

   def setCursor(self, db):
      self.db = db

   def setSearch(self, search):
      self.site = search

   def Collect(self, count):
      self.search.Search()

   def hasDater(self, login):
      if self.db:
         if self.re_secure_login.match(login):
            self.db.execute('SELECT login, oid from Dater WHERE login="login"')
            result = self.db.fetchall()
            if len(result):
               return True
      return False

   def getDater_interlocutors(self, dater):
      interlocutors = []
      if self.db:
         self.db.execute('SELECT userid from Conversation WHERE daterid="%d"' % (dater.id))
         data = self.db.fetchall()
         if len(data):
            for line in data:
               user = User()
               if user.fromID(line[0]):
                  interlocutors.append(user)
      return interlocutors

   def getUser_interlocutors(self, user):
      interlocutors = []
      if self.db:
         self.db.execute('SELECT daterid from Conversation WHERE userid="%d"' % (user.id))
         data = self.db.fetchall()
         if len(data):
            for line in data:
               dater = Dater()
               if dater.fromID(line[0]):
                  interlocutors.append(dater)
      return interlocutors

def main():
   pass

if __name__ == '__main__':
   main()